package com.uit.trip.tab_activity;

import java.util.ArrayList;
import java.util.List;

import com.uit.trip.R;
import com.uit.trip.TripApplication;
import com.uit.trip.bridge.Parser;
import com.uit.trip.bridge.Server;
import com.uit.trip.lazyloading.ImageLoader;
import com.uit.trip.models.Trip;
import com.uit.trip.tab_activity.childs.CommentActivity;
import com.uit.trip.utils.ProcessImage;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class Tab_TripSearchActivity extends Activity {

	private GridView gridView;
	private BroadcastSearch searchReceiver;
	public static final String SEARCH_NEW = "searchnew";
	private List<Trip> tripListSearch;
	private ImageAdapter adapter;
	private Server server;
	private EditText etSearch;
	private ProgressBar progressBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.tab_search);

		progressBar = (ProgressBar) findViewById(R.id.progressBarSearch);
		progressBar.setVisibility(View.INVISIBLE);
		tripListSearch = new ArrayList<Trip>();

		etSearch = (EditText) findViewById(R.id.etTabSearch);
		server = new Server(this);
		adapter = new ImageAdapter();
		searchReceiver = new BroadcastSearch();
		gridView = (GridView) findViewById(R.id.gridView1);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(onListener);
	}

	private void startProgressBar() {
		gridView.setVisibility(View.GONE);
		progressBar.setVisibility(View.VISIBLE);
		progressBar.setProgress(0);
		RelativeLayout layout = (RelativeLayout) findViewById(R.id.searchLayout);
		layout.setVisibility(View.VISIBLE);
	}

	private void hideProgressBar() {
		gridView.setVisibility(View.VISIBLE);
		RelativeLayout layout = (RelativeLayout) findViewById(R.id.searchLayout);
		layout.setVisibility(View.GONE);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		unregisterReceiver(searchReceiver);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		adapter.notifyDataSetChanged();
		registerReceiver(searchReceiver, new IntentFilter(SEARCH_NEW));
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();

	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btTabSearch:
			startProgressBar();
			TripApplication app = (TripApplication) getApplication();
			String userId = "1";
			userId = app.getUser().userId;
			String search = etSearch.getText().toString();
			etSearch.setText("");
			if (!etSearch.equals("")) {
				server.getLisSearch(userId, search);
			} else {
				Toast.makeText(this, "Please insert something",
						Toast.LENGTH_SHORT).show();
			}
			break;

		default:
			break;
		}
	}

	private OnItemClickListener onListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			// Toast.makeText(getApplicationContext(), "position:" + arg2,
			// Toast.LENGTH_SHORT).show();

		}
	};

	private class ImageAdapter extends BaseAdapter {

		public ImageLoader imageLoader = new ImageLoader(
				getApplicationContext());

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return tripListSearch.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return tripListSearch.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parentView) {
			// TODO Auto-generated method stub
			final Trip trip = tripListSearch.get(position);
			ImageView imgView;
			if (convertView == null) {
				imgView = new ImageView(getApplicationContext());
				imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				imgView.setLayoutParams(new GridView.LayoutParams(90, 90));
				imgView.setPadding(0, 0, 0, 0);

			} else {
				imgView = (ImageView) convertView;
			}

			if (trip.bitmap != null) {
				imgView.setImageBitmap(trip.bitmap);
			} else {
				if (trip.link != null && !trip.link.equals("")) {
					String link = trip.link;
					trip.bitmap = imageLoader.DisplayImage(link, imgView);
					Log.i("duongnx", "tab_location:" + position);
				}
			}
			final int index = position;
			imgView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					adapter.notifyDataSetChanged();
					Intent i = new Intent(getApplicationContext(),
							CommentActivity.class);
					i.putExtra("position", index);
					i.putExtra("type", TripApplication.TYPE_SEARCH);
					startActivity(i);
				}
			});
			return imgView;
		}

	}

	private Integer[] mThumbIds = { R.drawable.nhatrang, R.drawable.nhatrang,
			R.drawable.nhatrang, R.drawable.nhatrang, R.drawable.nhatrang,
			R.drawable.nhatrang };

	private class BroadcastSearch extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			hideProgressBar();
			Log.i("duongnx", "BroadcastSearch");
			Bundle extras = intent.getExtras();
			if (extras == null)
				return;
			Boolean result = extras.getBoolean("result");
			if (result) {
				String response = extras.getString("response");

				TripApplication app = (TripApplication) getApplication();
				tripListSearch = Parser.parseImageList(response);
				app.setTripList(tripListSearch, TripApplication.TYPE_SEARCH);
				adapter.notifyDataSetChanged();
				if (tripListSearch.size() == 0) {
					Toast.makeText(getApplicationContext(), "No items found",
							Toast.LENGTH_SHORT).show();
				}
			} else {
				Toast.makeText(getApplicationContext(), "Searching fail...",
						Toast.LENGTH_SHORT).show();
			}
		}

	}
}
